//
//  NSString+JwFormat.m
//  JwPack
//
//  Created by 陈警卫 on 2024-07-17.
//

#import "NSString+JwFormat.h"

@implementation NSString (JwFormat)

/**
 kCFNumberFormatterDecimalStyle (1,234.56)
 NSNumberFormatterPercentStyle 百分比样式(0.5 = 50%)
 NSNumberFormatterCurrencyStyle 货币样式($1,234.56)
 currencyCode 货币代码(USD)
 */
/**
  positiveFormat = @"$#,##0.00"
 
 $   表示货币符号
 #   表示数字位，当数字位不够时不会填充零
 ,   作为千位分隔符
 0   表示必须存在的数字位，如果数字位不够则填充零
 .00 表示两位小数，且必须显示
 
     123456.789 = "$123,456.79"。
 */

/// 数字样式格式
- (NSString *)jw_stringMoneyPositiveFormat:(NSString *)positiveFormat {
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc]init];
    formatter.positiveFormat = positiveFormat;
    NSString *money = [self jw_stringMoneyFormatter:formatter];
    return money;
}

/// 数字样式格式
- (NSString *)jw_stringMoneyNumberStyle:(NSNumberFormatterStyle)numberStyle {
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc]init];
    formatter.numberStyle = numberStyle;
    NSString *money = [self jw_stringMoneyFormatter:formatter];
    return money;
}

/// 数字样式格式
- (NSString *)jw_stringMoneyFormatter:(NSNumberFormatter *)formatter {
    double value = [self doubleValue];
    NSNumber *nv = [NSNumber numberWithDouble:value];
    NSString *money = [formatter stringFromNumber:nv];
    return money;
}

/// 银行卡号格式显示
- (NSString *)jw_stringBankCardNumberFormat {
    NSString *string = self;
    
    NSInteger groupSize = 4;
    NSInteger groupCount = (NSInteger)ceilf((CGFloat)string.length/groupSize);
    
    NSMutableArray *components = [[NSMutableArray alloc] init];

    for (NSInteger i = 0; i < groupCount; i++) {
        if (i*groupSize + groupSize > string.length) {
            [components addObject:[string substringFromIndex:i*groupSize]];
        } else {
            [components addObject:[string substringWithRange:NSMakeRange(i*groupSize, groupSize)]];
        }
    }
    NSString *groupedString = [components componentsJoinedByString:@" "];
    return groupedString;
}

/// 银行卡号格式加密显示
- (NSString *)jw_stringBankCardNumberEncryptFormat {
    NSString *string = self;
    NSInteger groupSize = 4;
    NSInteger groupCount = (NSInteger)ceilf((CGFloat)string.length/groupSize);
    
    NSMutableArray *components = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < groupCount; i++) {
        if (i*groupSize + groupSize > string.length) {
            [components addObject:[string substringFromIndex:i*groupSize]];
        } else {
            [components addObject:@"****"];
        }
    }
    NSString *groupedString = [components componentsJoinedByString:@" "];
    return groupedString;
}

/// 邮箱地址格式加密显示
- (NSString *)jw_stringEmailEncryptFormat {
    NSString *email = self;
    NSArray *emails = [email componentsSeparatedByString:@"@"];
    if (emails.count > 0) {
        NSString *head = emails.firstObject;
        if (head.length > 1) {
            head = [head substringToIndex:1];
        }
        NSString *encrypt = [NSString stringWithFormat:@"%@****@%@", head, emails.lastObject];
        return encrypt;
    } else {
        return self;
    }
}


@end
